from httprunner import HttpRunner,Config,Step,RunTestCase,RunRequest
from business.test_login import TestCaseLogin as TC
from time import sleep
from common.ConfigReader import ConfigReader
base_url = ConfigReader('config.ini').get_config('dev_base_url')
class API_Start(HttpRunner): # http://1231,123,123,132:8899
    config = Config("用例开始执行").base_url(base_url).verify(False)
    teststeps = [
        Step(
            RunTestCase("testcase start")
            .call(TC)
            .export(
                *['token',]
            )
        )
    ]
"""
电商网站：
管理员
游客
已登录用户
"""

def reset_token(token = TC): # 重置token
    API_Start.teststeps.clear()
    API_Start.teststeps.append(
            Step(
                RunTestCase("testcase start")
                .call(token)
                .export(
                    *['token',]
                )
            )
        )
headers = {'Authori-zation': "Bearer ${token}"}
def requests_obj(requests_name,method,url,request_header=headers,request_body=None):
    if method == "get": # ?2&asd
        return RunRequest(requests_name).get(url).with_headers(**request_header).extract().with_jmespath("body",'res_data')
    elif method =='post':
        return RunRequest(requests_name).post(url).with_headers(**request_header).with_json(request_body).extract().with_jmespath("body",'res_data')
    elif method =='delete':
        return RunRequest(requests_name).delete(url).with_headers(**request_header).with_json(request_body).extract().with_jmespath("body",'res_data')
    elif method =='put':
        return RunRequest(requests_name).put(url).with_headers(**request_header).with_json(request_body).extract().with_jmespath("body","res_data")

def obj_step(obj):
    return Step(obj)

def get_last_response(obj): # [obj1,obj2]
    API_Start.teststeps.extend(obj) # # [obj1,obj2]
    admin = API_Start()
    admin.test_start() # 开始执行
    sleep(1)
    res = admin.with_export(['token','res_data']).get_export_variables()
    return res











